package hoangbn.apps;

import hoangbn.apps.util.Variables;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MapActivity extends Activity {

	private static final String TAG = MapActivity.class.getName();

	float x, y;
	GoogleMap mMap;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		Bundle bundle = this.getIntent().getExtras();
		x = bundle.getFloat(Variables.X);
		y = bundle.getFloat(Variables.Y);
		// Config on google Map
		mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
				.getMap();

		LatLng latlngHCMUT = new LatLng(x, y);
		LatLng latlngCVTanPhuoc = new LatLng(10.774669f, 106.653038f);

		mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlngHCMUT, 18));

		mMap.addMarker(new MarkerOptions().position(latlngHCMUT).title(
				"Đại Học Bách Khoa"));
		mMap.addMarker(new MarkerOptions().position(latlngCVTanPhuoc).title(
				"Công viên tân phước"));
	}

	public void onClick_btnDirection(View v) {
		new AsyncTask<Void, Void, String>() {
			@Override
			protected String doInBackground(Void... params) {
				// TODO Auto-generated method stub
				LatLng latlngHCMUT = new LatLng(x, y);
				LatLng latlngCVTanPhuoc = new LatLng(10.774669f, 106.653038f);

				// Read simple JSON File
				try {
					JSONParser jp = new JSONParser();
					String strResult = jp.getJSONFromUrl(latlngHCMUT,
							latlngCVTanPhuoc);
					return strResult;
				} catch (Exception ex) {
					Log.e(TAG, ex.toString());
				}
				return null;
			}

			protected void onPostExecute(String result) {
				// Read simple JSON File
				try {
					JSONObject jsonObject = new JSONObject(result);

					JSONArray routesArray = jsonObject.getJSONArray("routes");
					// Grab the first route
					JSONObject route = routesArray.getJSONObject(0);
					// Take all legs from the route
					JSONArray legs = route.getJSONArray("legs");
					// Grab first leg
					JSONObject leg = legs.getJSONObject(0);
					// Grab array Steps
					JSONArray stepsArray = leg.getJSONArray("steps");

					ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();

					for (int i = 0; i < stepsArray.length(); i++) {
						JSONObject step = stepsArray.getJSONObject(i);
						JSONObject startLocation = step
								.getJSONObject("start_location");
						double lat = Double.parseDouble((startLocation
								.getString("lat")));
						double lng = Double.parseDouble((startLocation
								.getString("lng")));
						listGeopoints.add(new LatLng(lat, lng));

						JSONObject endLocation = step
								.getJSONObject("end_location");
						lat = Double
								.parseDouble((endLocation.getString("lat")));
						lng = Double
								.parseDouble((endLocation.getString("lng")));
						listGeopoints.add(new LatLng(lat, lng));
					}

					PolylineOptions rectLine = new PolylineOptions().width(12)
							.color(Color.BLUE); // Màu và độ rộng

					for (int i = 0; i < listGeopoints.size(); i++) {
						rectLine.add(listGeopoints.get(i));
					}

					mMap.addPolyline(rectLine);

				} catch (Exception ex) {
					Log.e(TAG, ex.toString());
				}
			};
		}.execute();

		// DataBaseHelper myDBHelper = new DataBaseHelper(this);
		// try {
		// myDBHelper.createDataBase();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// myDBHelper.openDataBase();
		// //myDBHelper.createDrink();
		// myDBHelper.close();
		// Toast toast = Toast.makeText(this.getApplicationContext(),
		// myDBHelper.getDrink(), Toast.LENGTH_SHORT);
		// toast.show();

	}
}
